package com.zack.gank.gankandroid.main.find;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.zack.gank.gankandroid.gankandroid.R;
import com.zack.gank.gankandroid.main.LzyFragment;
import com.zack.gank.gankandroid.main.find.adapter.SelectFindAdapter;
import com.zack.gank.gankandroid.main.find.bean.AndroidGank;
import com.zack.gank.gankandroid.utils.InterfaceUtils;
import com.zack.gank.gankandroid.utils.StringUtils;

/**
 * Created by Android on 2017/7/13.
 */
public class IosFindFragment extends LzyFragment implements FindAndroidView {

    private RefreshLayout refreshLayout;
    private FindAndroidPresenter findAdnroidPresenter;
    private int pager=1;
    private RecyclerView rlvAndroid;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view= inflater.inflate(R.layout.fg_find_android,container,false);
             refreshLayout= (RefreshLayout) view.findViewById(R.id.refreshLayout);
             rlvAndroid= (RecyclerView) view.findViewById(R.id.rlv_android);
        /*//设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                return new ClassicsHeader(context);//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });*/

        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                return new ClassicsFooter(context);//指定为经典Footer，默认是 BallPulseFooter
            }
        });
             return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        /*refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {

            }
        });*/


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshLayout) {
                refreshLayout.finishLoadmore(2000);

            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(2000);
            }
        });
    }

    @Override
    protected void loadData() {
        Logger.v("IosFindFragment");
        if(findAdnroidPresenter==null){
            findAdnroidPresenter=new FindAndroidPresenter(this);
        }
        findAdnroidPresenter.setGankNumPager(InterfaceUtils.typeIosData,StringUtils.number,Integer.toString(pager));

    }

    @Override
    public void onSucess(AndroidGank todayGank) {

        if(todayGank!=null){
            rlvAndroid.setAdapter(new SelectFindAdapter(getContext(),todayGank.results));
            rlvAndroid.setLayoutManager(new LinearLayoutManager(getContext()));
        }
    }

    @Override
    public void onFile(Throwable t) {

    }
}
